package com.qf.j2112.controller;

import com.qf.j2112.anno.JWTAnno;
import com.qf.j2112.po.SysUser;
import com.qf.j2112.service.SysUserService;
import com.qf.j2112.utils.JwtUtil;
import com.qf.j2112.utils.PageUtil;
import com.qf.j2112.vo.ActionResultVO;
import com.qf.j2112.vo.LogInVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api")
@CrossOrigin
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    //用户端
    //用户登录
    @JWTAnno(name = "用户登录")
    @PostMapping ("/login")
    public ActionResultVO login(@RequestBody LogInVO logInVO){
        log.info("登录信息为：",logInVO);
        ActionResultVO vo=null;
        SysUser sysUser=sysUserService.findUserByUname(logInVO.getUserName());
        if (!ObjectUtils.isEmpty(sysUser)&&sysUser.getPassword().equals(logInVO.getPassword())){
            String token= JwtUtil.getToken(String.valueOf(sysUser.getUid()));
            vo=new ActionResultVO(200,"登录成功",token);
        }else {
            System.out.println("用户名或密码错误");
            vo=new ActionResultVO(-200,"登录失败,用户名或密码错误",null);
        }
        return vo;
    }


    /*
    //以下后台增删查改
    */
    //获取全部用户列表
    @JWTAnno(name = "获取全部用户列表")
    @GetMapping("/users")
    public ActionResultVO getUserList(PageUtil pageUtil){
        ActionResultVO vo=null;
        List<SysUser> userList = sysUserService.findAllSysUser(pageUtil);
        int num=sysUserService.countUser();
        if (!ObjectUtils.isEmpty(userList)){
             vo=new ActionResultVO(200,"查询全部用户成功",userList,num);
        }else {
            vo=new ActionResultVO(-200,"查询全部用户失败",null);
        }
        return vo;
    }

    //获取单个用户的信息
    @JWTAnno(name = "获取单个用户的信息")
    @GetMapping("/user")
    public ActionResultVO getOneUser(int uid){
        ActionResultVO vo=null;
        SysUser user=sysUserService.findUserByUid(uid);
        if (!ObjectUtils.isEmpty(user)){
            vo=new ActionResultVO(200,"查询单个用户成功",user);
        }else {
            vo=new ActionResultVO(-200,"查询单个用户失败",null);
        }
        return vo;
    }

    @PostMapping("/addUser")
    @JWTAnno(name = "新增用户信息")
    public ActionResultVO addUser(@RequestBody SysUser sysUser){
        sysUser.setCreateTime(new Date());
        ActionResultVO vo=null;
        //String spass= new SimpleHash("MD5",sysUser.getPassword(),sysUser.getLoginName(),1024).toString();
        //sysUser.setPassword(spass);
        System.out.println("添加前："+sysUser);
        SysUser user=sysUserService.addSysUser(sysUser);
        System.out.println("添加后："+user);
        if(!ObjectUtils.isEmpty(user)){
            vo=new ActionResultVO(200,"添加用户成功",user);
        }else {
            vo=new ActionResultVO(-200,"添加用户失败",user);
        }
        return vo;
    }

    @DeleteMapping("/delUser")
    @JWTAnno(name = "删除一个用户")
    public ActionResultVO deleteUser(int uid){
        ActionResultVO vo=null;
        if(sysUserService.deleteSysUser(uid)){
            vo=new ActionResultVO(200,"删除用户成功",null);
        }else {
            vo=new ActionResultVO(-200,"删除用户失败",null);
        }
        return vo;
     }

     //只有前台可以个人信息，后台系统管理员不修改
    //真实姓名不能修改
    //动态sql
    @PutMapping ("/updateUser")
    @JWTAnno(name = "修改一个用户的信息")
    public ActionResultVO updateUser(@RequestBody SysUser sysUser){
       log.info("用户：",sysUser.toString());
        System.out.println(sysUser);
       sysUser.setCreateTime(new Date());
        ActionResultVO vo=null;
        if(sysUserService.updateSysUser(sysUser)){
            vo=new ActionResultVO(200,"修改用户成功",null);
        }else {
            vo=new ActionResultVO(-200,"修改用户失败",null);
        }
        return vo;
    }


    @PutMapping ("/changeUserState")
    @JWTAnno(name = "修改一个用户的状态")
    public ActionResultVO changeUserState(@RequestBody SysUser sysUser){
        log.info("用户：",sysUser);
        sysUser.setCreateTime(new Date());
        ActionResultVO vo=null;
        if(sysUserService.updateSysUser(sysUser)){
            vo=new ActionResultVO(200,"修改用户状态成功",null);
        }else {
            vo=new ActionResultVO(-200,"修改用户状态失败",null);
        }
        return vo;
    }

}
